﻿
// DCMDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "DCM.h"
#include "DCMDlg.h"
#include "afxdialogex.h"
#include "CDCMEngine.h"
#include "obj.h"
#include "CHttpSrvThread.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CDCMEngine* g_pEngine = NULL;
CDCMDlg* g_pDCMDlg = NULL;
extern Page2* g_pPage2;



class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()

};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{

}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);


}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)


END_MESSAGE_MAP()


// CDCMDlg 对话框


#define TIME_UPDATE_WND 1




CDCMDlg::CDCMDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DCM_DIALOG, pParent)
{
	//m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_strDMKey = _T("mh84909b3bf80d45c618136887775ccc90d27d7");
	m_bRegDm = false;
	m_strCmd = _T("注册");
	m_strParam = _T("");
	m_checkIndex = 0; //默认设置0

}

void CDCMDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_WND, m_lstWnd);
	DDX_Control(pDX, IDC_TAB_WND, m_tab_wnd);
	//日志
	DDX_Control(pDX, IDC_TRACE_MESSAGE, m_TraceServiceControl);
}

BEGIN_MESSAGE_MAP(CDCMDlg, CDialogEx)
	ON_MESSAGE(WM_RESTART_MSG, OnRestartMessage)
	ON_MESSAGE(WM_SWITCH_MSG, OnSwitchMessage)
	ON_MESSAGE(WM_RESTARTGAME_MSG, OnRestartGameMessage)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_WM_CLOSE()
	ON_NOTIFY(NM_RCLICK, IDC_LIST_WND, &CDCMDlg::OnNMRClickListWnd)
	ON_COMMAND(ID_32771, &CDCMDlg::单个启动)
	ON_COMMAND(ID_Memu_Suspend_Choosed, &CDCMDlg::单个暂停)
	ON_COMMAND(ID_32773, &CDCMDlg::单个恢复)
	ON_COMMAND(ID_32774, &CDCMDlg::单个停止)
	ON_COMMAND(ID_32775, &CDCMDlg::单个关闭)
	ON_COMMAND(ID_32776, &CDCMDlg::单个重启)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_WND, &CDCMDlg::OnNMDblclkListWnd)
	ON_NOTIFY(NM_CLICK, IDC_LIST_WND, &CDCMDlg::OnNMClickListWnd)
END_MESSAGE_MAP()


// CDCMDlg 消息处理程序

BOOL CDCMDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	
	SetWindowText(_T("Ai卡牌助手V1.5"));
	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	//获取当前目录
	TCHAR szPath[256] = {0};
	CWHService::GetWorkDirectory(szPath, 256);
	m_strWorkPath = szPath;


	//显示列表框样式设置
	DWORD dwStyle = m_lstWnd.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT;
	dwStyle |= LVS_EX_GRIDLINES;
	dwStyle |= LVS_EX_CHECKBOXES;
	m_lstWnd.SetExtendedStyle(dwStyle);

	m_lstWnd.InsertColumn(0, _T("窗口序号"), LVCFMT_CENTER, 60);
	m_lstWnd.InsertColumn(1, _T("窗口标题"), LVCFMT_CENTER, 120);
	m_lstWnd.InsertColumn(2, _T("账号"), LVCFMT_CENTER, 180);
	m_lstWnd.InsertColumn(3, _T("主任务"), LVCFMT_CENTER, 120);
	m_lstWnd.InsertColumn(4, _T("执行任务"), LVCFMT_CENTER, 120);
	m_lstWnd.InsertColumn(5, _T("角色信息"), LVCFMT_CENTER, 260);

	g_pEngine = new CDCMEngine();
	g_pEngine->Init();
	g_pDCMDlg = this;



	m_tab_wnd.AddPage(_T("全局设置"), &m_Page1, IDD_Page1);
	m_tab_wnd.AddPage(_T("提现设置"), &m_Page2, IDD_Page2);



	SetTimer(TIME_UPDATE_WND, 1000, NULL);

	

	m_hDMInitThread = AfxBeginThread(CDMEngineThread, this, THREAD_PRIORITY_NORMAL, 0, 0, NULL);
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CDCMDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CDCMDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CDCMDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CDCMDlg::Start()
{
	
		m_strCmd = _T("全部启动");
	
}

void CDCMDlg::Suspend()
{

	m_strCmd = _T("全部暂停");
}

void CDCMDlg::Stop()
{
	m_strCmd = _T("全部停止");
}

void CDCMDlg::Resume()
{
	m_strCmd = _T("全部恢复");
}

void CDCMDlg::单个启动(){

	
   m_strCmd = _T("选中启动");
	
	
}

void CDCMDlg::单个暂停()
{
	m_strCmd = _T("选中暂停");
}

void CDCMDlg::单个恢复()
{
	m_strCmd = _T("选中恢复");
}


void CDCMDlg::单个停止()
{
	m_strCmd = _T("选中停止");
}


void CDCMDlg::单个关闭()
{
	m_strCmd = _T("选中关闭");
}


void CDCMDlg::单个重启()
{
	m_strCmd = _T("选中重启");
}




//定时更新界面函数
void CDCMDlg::OnTimer(UINT_PTR nIDEvent)
{
	if (nIDEvent == TIME_UPDATE_WND)
	{
		g_pEngine->GetWndList();

		if (g_pEngine->m_arrWnd.GetCount() > 0)
		{
			for (int i = 0; i < g_pEngine->m_arrWnd.GetCount(); i++)
			{
				int id = (int)g_pEngine->m_arrWnd[i]->id;

				//是否已开启
				if (IsWndExist(id)) //更新这行
				{
					m_lstWnd.SetItemText(id-1, 1, g_pEngine->m_arrWnd[i]->strTitle);

					tagGameStatus* pStatus = g_pEngine->GetGameStatus(id);
					if (pStatus)
					{

						if (g_pEngine->GetGameStatus(id)->mainName)
							m_lstWnd.SetItemText(id-1, 3, g_pEngine->GetGameStatus(id)->mainName);

						if (g_pEngine->GetGameStatus(id)->strName) {
							m_lstWnd.SetItemText(id-1, 4, g_pEngine->GetGameStatus(id)->strName);
						}

						//角色信息
						if (g_pEngine->GetGameStatus(id)->info) {
							m_lstWnd.SetItemText(id - 1, 5, g_pEngine->GetGameStatus(id)->info);
						}
					}
				}
				else //插入一行
				{
					if (id == 0) return;
					int iRow = m_lstWnd.GetItemCount();
					m_lstWnd.InsertItem(iRow, _T(""));

					m_lstWnd.SetItemText(iRow, 0, I2S((int)g_pEngine->m_arrWnd[i]->id));
					m_lstWnd.SetItemText(iRow, 1, g_pEngine->m_arrWnd[i]->strTitle);
		
				}
			}
		}

		//KillTimer(TIME_UPDATE_WND);
	}

	CDialogEx::OnTimer(nIDEvent);
}

bool CDCMDlg::IsWndExist(int id)
{
	for (int i = 0; i < m_lstWnd.GetItemCount(); i++)
	{
		int idTmp = _ttoi(m_lstWnd.GetItemText(i, 0));
		if (id == idTmp)
			return true;
	}

	return false;
}




//获取列表选取
CString CDCMDlg::GetCheckList(CArray<int>& arrCheckID)
{
	CString strID = _T("");
	for (int i = 0; i < m_lstWnd.GetItemCount(); i++)
	{
		if (m_lstWnd.GetCheck(i))
		{
			CString strTmp = m_lstWnd.GetItemText(i, 0);
			arrCheckID.Add(_ttoi(strTmp));
			strID += strTmp + _T("-");
		}
	}

	return strID;
}




//游戏主线程
UINT CDCMDlg::CDMEngineThread(LPVOID pParam)
{
	CDCMDlg* pThis = (CDCMDlg*)pParam;

	CoInitializeEx(NULL, 0);

	
	
	LogD(_T("欢迎使用Ai卡牌助手V1.5"));


	while (true)
	{

		if (pThis->m_strCmd == _T("退出"))
		{
			break;
		}
		else if (pThis->m_strCmd == _T("注册"))
		{
			HMODULE hDmReg = LoadLibrary(pThis->m_strWorkPath + _T("/DmReg.dll"));
			if (hDmReg == NULL)
			{
				LogE(_T("DmReg.dll文件不存在"));
				continue;
			}

			typedef long (CALLBACK* TypeSetDllPathW)(WCHAR* szPath, long mode);
			TypeSetDllPathW pfnSetDllPathW = (TypeSetDllPathW)GetProcAddress(hDmReg, "SetDllPathW");
			if (pfnSetDllPathW == NULL)
				continue;

			CString strDmPath = pThis->m_strWorkPath + _T("/dm.dll");
			long iRet = pfnSetDllPathW((WCHAR*)strDmPath.GetString(), 0);

			dmsoft* pDm = new dmsoft();
			long dm_ret = pDm->Reg(pThis->m_strDMKey, _T("mmmc2vn4w035fb7"));
			if (dm_ret != 1)
			{
				LogD(_T("大漠注册失败! 返回值:%d"), dm_ret);
				if (pDm)
					delete pDm;

				continue;
			}
			else {
				LogN(_T("大漠注册成功! 版本号: %s..."), pDm->Ver());
			}

			if (pDm)
				delete pDm;
		}
		else if (pThis->m_strCmd == _T("选中启动"))
		{

		
		    g_pEngine->Start(pThis->m_checkIndex);
			Sleep(1000);
		}
		else if (pThis->m_strCmd == _T("全部启动")) {
			

			
				CArray<int> arrID;

				pThis->GetCheckList(arrID);

				if (arrID.GetCount() > 0) {
					//勾选了

					for (int i = 0; i < arrID.GetCount(); i++) {
						g_pEngine->Start(arrID.GetAt(i));
						Sleep(1000);
					}
				}else {

					for (int i = 0; i < g_pEngine->m_arrWnd.GetCount(); i++) {
						g_pEngine->Start(g_pEngine->m_arrWnd[i]->id);
						Sleep(1000);
					}
				}

		


		}
		else if (pThis->m_strCmd == _T("选中停止"))
		{
			g_pEngine->Stop(pThis->m_checkIndex);
			Sleep(1000);
		}
		else if (pThis->m_strCmd == _T("全部停止")) {

			CArray<int> arrID;
			pThis->GetCheckList(arrID);

			if (arrID.GetCount() > 0) {
				//勾选了

				for (int i = 0; i < arrID.GetCount(); i++) {
					g_pEngine->Stop(arrID.GetAt(i));
					Sleep(1000);
				}
			}
			else {
				g_pEngine->Stop();
			}
		}
		else if (pThis->m_strCmd == _T("选中暂停"))
		{
			g_pEngine->Suspend(pThis->m_checkIndex);
			Sleep(1000);
		}
		else if (pThis->m_strCmd == _T("全部暂停"))
		{

			CArray<int> arrID;
			pThis->GetCheckList(arrID);

			if (arrID.GetCount() > 0) {
				//勾选了

				for (int i = 0; i < arrID.GetCount(); i++) {
					g_pEngine->Suspend(arrID.GetAt(i));
					Sleep(1000);
				}
			}
			else {
				g_pEngine->Suspend();
			}
		}
		else if (pThis->m_strCmd == _T("选中恢复"))
		{

			g_pEngine->Resume(pThis->m_checkIndex);
			Sleep(1000);
		}
		else if (pThis->m_strCmd == _T("全部恢复"))
		{
		CArray<int> arrID;
	    	pThis->GetCheckList(arrID);

		if (arrID.GetCount() > 0) {
			//勾选了

			for (int i = 0; i < arrID.GetCount(); i++) {
				g_pEngine->Resume(arrID.GetAt(i));
				Sleep(1000);
			}
		}
		else {
			g_pEngine->Resume();
		}
		}
		else if (pThis->m_strCmd == _T("重启"))
		{
			g_pEngine->Restart(_ttoi(pThis->m_strParam));
		}
		else if (pThis->m_strCmd == _T("选中重启")) {
			g_pEngine->Restart(pThis->m_checkIndex);
		}
		else if (pThis->m_strCmd == _T("切换"))
		{
			CStringArray arrID;
			Split(pThis->m_strParam, arrID, _T("-"));
			if (arrID.GetCount() != 2)
			{
				LogE(_T("切换命令参数错误: %s"), pThis->m_strParam);
			}
			else
			{
				g_pEngine->Switch(_ttoi(arrID[0]), _ttoi(arrID[1]));
			}
		}
		else if (pThis->m_strCmd == _T("重启游戏")) {
			g_pEngine->KillApk(_ttoi(pThis->m_strParam));
			Sleep(5000);
			g_pEngine->RunApk(_ttoi(pThis->m_strParam));
		}


		pThis->m_strCmd = _T("");
		pThis->m_strParam = _T("");
		Sleep(1000);

	}
	CoUninitialize();



	return 0;
}

void CDCMDlg::OnClose()
{
	m_hDMInitThread->Delete();

	if (g_pEngine)
		delete g_pEngine;

	CDialogEx::OnClose();
}

LRESULT CDCMDlg::OnRestartMessage(WPARAM wParam, LPARAM lParam)
{
	m_strCmd = _T("重启");

	CString strID;
	strID.Format(_T("%d"), wParam);
	m_strParam = strID;



	return 0;
}

LRESULT CDCMDlg::OnRestartGameMessage(WPARAM wParam, LPARAM lParam)
{
	m_strCmd = _T("重启游戏");

	CString strID;
	strID.Format(_T("%d"), wParam);
	m_strParam = strID;

	return 0;
}

LRESULT CDCMDlg::OnSwitchMessage(WPARAM wParam, LPARAM lParam)
{
	m_strCmd = _T("切换");

	CString strID;
	strID.Format(_T("%d-%d"), wParam, lParam);
	m_strParam = strID;

	return 0;
}






void CDCMDlg::OnCancel()
{



	exit(0);
	
}



void CDCMDlg::OnNMRClickListWnd(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);


	// TODO: 在此添加控件通知处理程序代码
	CPoint ptCurSel(0, 0);
	GetCursorPos(&ptCurSel);
	//转换为当前控件坐标系的坐标
	m_lstWnd.ScreenToClient(&ptCurSel);
	LVHITTESTINFO HitTestInfo;
	HitTestInfo.pt = ptCurSel;
	HitTestInfo.flags = LVHT_ABOVE;
	//判断坐标命中第几项
	m_checkIndex = m_lstWnd.SubItemHitTest(&HitTestInfo)+1;
	//如果有选中项
	//弹出菜单
	CMenu menu;
	menu.LoadMenu(IDR_MENU1);
	CPoint point;
	GetCursorPos(&point);
	CMenu* pSubMenu = menu.GetSubMenu(0);
	pSubMenu->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this);

	*pResult = 0;
}


void CDCMDlg::getGameIni() {
	g_pPage2->setInfo(m_checkIndex-1);

}

//左键双击
void CDCMDlg::OnNMDblclkListWnd(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;


	CPoint ptCurSel(0, 0);
	GetCursorPos(&ptCurSel);
	//转换为当前控件坐标系的坐标
	m_lstWnd.ScreenToClient(&ptCurSel);
	LVHITTESTINFO HitTestInfo;
	HitTestInfo.pt = ptCurSel;
	HitTestInfo.flags = LVHT_ABOVE;
	//判断坐标命中第几项
	m_checkIndex = m_lstWnd.SubItemHitTest(&HitTestInfo) + 1;
	getGameIni();
	
}


void CDCMDlg::OnNMClickListWnd(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
	CPoint ptCurSel(0, 0);
	GetCursorPos(&ptCurSel);
	//转换为当前控件坐标系的坐标
	m_lstWnd.ScreenToClient(&ptCurSel);
	LVHITTESTINFO HitTestInfo;
	HitTestInfo.pt = ptCurSel;
	HitTestInfo.flags = LVHT_ABOVE;
	//判断坐标命中第几项
	m_checkIndex = m_lstWnd.SubItemHitTest(&HitTestInfo) + 1;
	getGameIni();
}
